// pages/order/index.js
const { baseUrl } = require('../../globalData')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userData: {},
        payList: [],
        payVal: 0
    },
    // 用户信息获取
    getUserData() {
        const userData = wx.getStorageSync('userAddr')
        this.setData({
            userData
        })
    },
    // 购物车商品取出，只要提前勾选的商品
    getSelectedGoodsList() {
        const cartList = wx.getStorageSync('cartList')
        // 筛选数据
        const arr = cartList.filter(item => {
            return item.isSelected === true
        })
        this.setData({
            payList: arr
        })
        this.getAllPayValue()
    },
    // 计算总价
    getAllPayValue() {
        let val = 0
        const arr = this.data.payList
        arr.forEach(item => {
            val += item.pay
        })
        this.setData({
            payVal: val
        })
    },
    // 下单请求
    // 参数：布尔，是否付款
    userPay(boolean) {
        // 从缓存中删除已经购买的数据，其实就没选的重新存给缓存
        const cartList = wx.getStorageSync('cartList')
        const arr = cartList.filter(item => {
            return item.isSelected === false
        })
        wx.setStorageSync('cartList', arr)
        // 下单请求
        const userId = wx.getStorageSync('userId')
        // 完整收货地址
        const addr = this.data.userData.provinceName + this.data.userData.cityName + this.data.userData.countyName + this.data.userData.detailInfo
        const token = wx.getStorageSync('token')
        wx.request({
            url: baseUrl + '/pay',
            data: {
                buyList: this.data.payList,
                userId,
                payAll: this.data.payVal,
                isPay: boolean,
                userInfo: {
                    userName: this.data.userData.userName,
                    userTel: this.data.userData.telNumber,
                    userAddr: addr
                }
            },
            header: {
                'content-type': 'application/json',
                token
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: (res) => {
            }
        })
    },
    // 交钱
    submitOrder() {
        // 模拟付款，点击按钮正常就是生成了一个订单了
        wx.showModal({
            title: '提示',
            content: '确认购买？',
            success: (res) => {
                if (res.confirm) {
                    wx.showLoading({
                        title: '付款中',
                        mask: true,
                        success: (result) => {
                            this.userPay(true)
                        }
                    })
                    setTimeout(function () {
                        wx.hideLoading()
                        wx.redirectTo({
                            url: '/pages/orderList/index?index=3'
                        })
                    }, 4000)
                } else {
                    this.userPay(false)
                    wx.redirectTo({
                        url: '/pages/orderList/index?index=2'
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getUserData()
        this.getSelectedGoodsList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})